package com.atguigu.auth.service.impl;

import com.atguigu.auth.mapper.SysRoleMapper;
import com.atguigu.auth.mapper.SysUserRoleMapper;
import com.atguigu.auth.service.SysRoleService;
import com.atguigu.auth.service.SysUserRoleService;
import com.atguigu.model.system.SysRole;
import com.atguigu.model.system.SysUserRole;
import com.atguigu.vo.system.AssginRoleVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author Yang
 * @Date 2023/3/17 20:50
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {


    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

//    findRoleByAdminId
    // 根据用户获取角色数据
    @Transactional
    @Override
    public Map<String, Object> findRoleByAdminId(Long userId) {

        //1 查询所有角色, 返回List集合
        List<SysRole> allRolesList = this.list();
        //2 根据userId 查询角色用户关系表,查询UserID对应角色ID
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRole::getUserId, userId);
        List<SysUserRole> existUserRoleList = sysUserRoleService.list(wrapper);
        //2.1 根据用户角色表 拿到角色ID
        List<Long> existRoleIdList = existUserRoleList.stream().map(m -> m.getRoleId()).collect(Collectors.toList());
        //3 根据查询所有角色ID, 找到对应角色信息. 根据角色id到所有角色的list集合进行比较
        //              优雅
        List<SysRole> assignRoleList = allRolesList.stream().filter(m -> existRoleIdList.contains(m.getId())).collect(Collectors.toList());
        //4 把得到的两部分数据进行MAP封装
        Map<String, Object> roleMap = new HashMap<>();
        roleMap.put("assginRoleList", assignRoleList);
        roleMap.put("allRolesList", allRolesList);
        return roleMap;
    }

    // 根据用户分配角色
    @Transactional
    @Override
    public void doAssign(AssginRoleVo assginRoleVo) {
        // 先根据ID删除, 在添加
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRole::getUserId,assginRoleVo.getUserId());
        sysUserRoleService.remove(wrapper);
        // 然后添加角色
        List<Long> roleIdList = assginRoleVo.getRoleIdList();
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(assginRoleVo.getUserId());
        if(!ObjectUtils.isEmpty(roleIdList)){
            for (Long roleId : roleIdList) {
                sysUserRole.setRoleId(roleId);
                sysUserRoleService.save(sysUserRole);
            }
        }
    }
}
